//
//  NetworkTools.swift
//  微博
//
//  Created by mac on 15/12/20.
//  Copyright © 2015年 mac. All rights reserved.
//

import AFNetworking

//创建枚举类型的数据类型来判断是什么请求
enum HTTPMethed :String {
    case GET = "GET"
    case POST = "POST"
}

//反域名
let dataErrorDomain = "com.baidu.data.error"
class NetworkTools: AFHTTPSessionManager {
    //创建一个网络单例
    static let sharedTools: NetworkTools = {
        let urlStr = "https://api.weibo.com/"
        let baseUrl = NSURL(string: urlStr)
       let tool = NetworkTools(baseURL: baseUrl)
        //设置content类型
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
   
    
    //网络请求代码的封装
    func requestJSONDict(methed:HTTPMethed, urlString:String,parameters:[String:String]?,finished:(result:[String:AnyObject]?,error:NSError?)->()) {
        //创建单例
//        let manager = NetworkTools.sharedTools
        
        
        //判断是什么类型来创建对应什么类型的方法
        if methed == HTTPMethed.POST{
        
        POST(urlString, parameters:parameters , progress: { (progress) -> Void in
            //预留进度
            print(progress)
            }, success: { (task, result) -> Void in
//                print(result)
                //返回字典类型的数据
                guard let dict = result as? [String : AnyObject] else {
                    //自定义错误
                    let myError = NSError(domain: dataErrorDomain, code: -12315, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                    //错误的回调
                    finished(result: nil, error: myError)
                    return
                }
                //正确的回调
                finished(result: dict, error: nil)
                
            }) { (task, error) -> Void in
                print(error)
                //错误的回调
                finished(result: nil, error: error)
        }
        }else{
            GET(urlString, parameters:parameters , progress: { (progress) -> Void in
                //预留进度
                print(progress)
                }, success: { (task, result) -> Void in
//                    print(result)
                    //返回字典类型的数据
                    guard let dict = result as? [String : AnyObject] else {
                        //自定义错误
                        let myError = NSError(domain: dataErrorDomain, code: -12315, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                        //错误的回调
                        finished(result: nil, error: myError)
                        return
                    }
                    //正确的回调
                    finished(result: dict, error: nil)
                    
                }) { (task, error) -> Void in
                    print(error)
                    //错误的回调
                    finished(result: nil, error: error)
            }
        }
    }
    func setImageData(urlString:String,parameters:[String:String]?,imageData: NSData,finished:(result:[String:AnyObject]?,error:NSError?)-> ()) {
        //创建单例
        let manager = NetworkTools.sharedTools
        manager.POST(urlString, parameters: parameters,constructingBodyWithBlock: { (formData) -> Void in
            formData.appendPartWithFileData(imageData, name: "pic", fileName: "lt", mimeType: "image/jpeg")
            }, progress: { (progress) -> Void in
                print(progress)
            }, success: { (task, result) -> Void in
                guard let dict = result as? [String : AnyObject] else {
                    //自定义错误
                    let myError = NSError(domain: dataErrorDomain, code: -12315, userInfo: [NSLocalizedDescriptionKey : "数据不合法"])
                    //错误的回调
                    finished(result: nil, error: myError)
                    return
                }
                finished(result: dict, error: nil)
            }) { (task, error) -> Void in
                finished(result: nil, error: error)
        }
    }
    
    
    
    
}
